<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/5/19
 * Time: 10:02
 */

namespace App\Rep;


use App\Models\OrderLogistics;

class OrderLogisticsRep
{
    //物流信息

    /**
     * 添加物流发货信息
     * $finish_product_id
     * $order_id
     * $logistics_status
     * $to_be_sent_num
     * $remark
     * $deliveryman
     */
    public function addOrderLogistics($finish_product_id,$order_id,$logistics_status,$remark,$deliveryman,$logistics_info,$payment='',$received_money=0,$image1 = '',$image2 = ''){
        //添加发货记录
//        $insertArr = [
//            'finish_product_id'=>$finish_product_id,
//            'order_id'=>$order_id,
//            'logistics_status'=>$logistics_status,
//            'delivery_time' => date('Y-m-d H:i:s'),
//            'num'=>$to_be_sent_num,
//            'to_be_sent_num'=>$to_be_sent_num,
//            'remark' => $remark
//        ];
//        return OrderLogistics::insert($insertArr);
        $model = new OrderLogistics();
        $model->finish_product_id=$finish_product_id;
        $model->order_id=$order_id;
        $model->logistics_status=$logistics_status;
        $model->delivery_time= date('Y-m-d');
        $model->remark=$remark;
        //$model->deliveryman = $deliveryman;
        $model->logistics_info = $logistics_info;
        $model->image1=$image1;
        $model->image2=$image2;
        $model->received_money = $received_money;
        if(!empty($payment)){
            $model->payment=$payment;
        }
        $model->save();
        return $model;

    }



    /**
     * 更新物流发货信息
     * $order_logistics_id 物流信息id
     * $logistics_status   物流状态  1:自提2：物流
     * $logistics_info     物流详情
     * $remark             物流备注
     */
    public function updateOrderLogistics($order_logistics_id,$logistics_status,$logistics_info,$remark){
        $OrderLogistics = OrderLogistics::find($order_logistics_id);
        $OrderLogistics->sent_num = $OrderLogistics->to_be_sent_num;
        $OrderLogistics->to_be_sent_num = 0;
        $OrderLogistics->logistics_status = $logistics_status;
        if(!empty($logistics_info)){
            $OrderLogistics->logistics_info = $logistics_info;
        }
        $OrderLogistics->delivery_time = date("Y-m-d H:i:s");
        if(!empty($remark)){
            $OrderLogistics->remark = $remark;
        }

        $OrderLogistics->save();
        return $OrderLogistics;
    }

    /**
     * 获取物流待发件数
     * $order_logistics_id
     */
    public function getToBeSentNum($order_logistics_id){
        $OrderLogistics = OrderLogistics::find($order_logistics_id);
        return $OrderLogistics->to_be_sent_num;
    }

    //待发货，已发货，已签收
    public function getOrderLogisticsListRep($params){
        $list = OrderLogistics::Search($params)
            ->with(['order'=>function($q){
                $q->with(['color'=>function($q){
                    return $q->select('id','color');
                },'product'=>function($q){
                    return $q->select('id','product_name');
                },'customer'=>function($q){
                    return $q->select('id','customer_name','customer_address','contact_phone');
                },'modelNumber'=>function($q){
                    return $q->select('id','model_number','file_id')->with(['specification','file']);
                },'orderChild'=>function($q){
                    return $q->with(['job_type','employee']);
                },'specification'=>function($q){
                    return $q->select('id','specification');
                },'accountExecutive'=>function($q){
                    return $q->select('id','company_name');
                },'finish_product'=>function($q){
                    return $q->select('order_id','num','warehouse_id','goods_shelf_id');
                },'admin_user'=>function($q){
                    return $q->select('id','company_name');
                },'remark']);
            }])
            ->orderBy('delivery_time','desc')->paginate($params['per_page']??10);
        return $list;
    }

    public function updateOtherLogisticsStatus($id){
        $logistics = OrderLogistics::find($id);
        $other = OrderLogistics::where('order_id',$logistics->order_id)
            ->where('finish_product_id',$logistics->finish_product_id)
            ->update(['status'=>2]);
    }
}
